
package CCT220_Trabalho04_13106.control;


import CCT220_Trabalho04_13106.view.LimitePrincipal;


/**
 * </br>******************************
 * </br>Project: CCT220
 * </br>Package: CCT220_Trabalho04_13106.control
 * </br>Class: ControlePrincipal
 * </br>Last Change: 08/10/2011 11:22:37 by marcel
 * </br>Description: Class that control the operations of all entities
 * </br>******************************</br>
 * 
 * @author marcel
 * @author 13106
 * 
 */
public class ControlePrincipal {

	/**
	 * LimitePrincipal
	 */
	private final LimitePrincipal limPrincipal;
	/**
	 * ControleEstudante
	 */
	private ControleEstudante     ctrEstudante;
	/**
	 * ControleDisciplina
	 */
	private ControleDisciplina    ctrDisciplina;
	/**
	 * ControleTurma
	 */
	private ControleTurma         ctrTurma;
	/**
	 * ControleAssentamento
	 */
	private ControleAssentamento  ctrAssentamento;


	/**
	 * </br>******************************
	 * </br>Constructor for
	 * </br>Package: CCT220_Trabalho04_13106.control
	 * </br>Class: ControlePrincipal
	 * </br>LastChange: 08/10/2011 11:22:38 by marcel
	 * </br>******************************</br>
	 * 
	 */
	public ControlePrincipal() {

		this.limPrincipal = new LimitePrincipal();
		try {
			ctrEstudante = new ControleEstudante();
			ctrDisciplina = new ControleDisciplina();
			ctrTurma = new ControleTurma(this);
			ctrAssentamento = new ControleAssentamento(this);
		} catch (Exception ex) {
			limPrincipal.showErrorMessage("Erro na abertura do arquivo!!");
		}
	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Trabalho04_13106.control
	 * </br>Class: ControlePrincipal
	 * </br>Method: getControleEstudante
	 * </br>Last Change: 08/10/2011 11:22:40 by marcel
	 * </br>Description: Getter for ctrEstudante
	 * </br>******************************</br>
	 * 
	 * @return the ctrEstudante
	 */
	public ControleEstudante getControleEstudante() {

		return ctrEstudante;
	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Trabalho04_13106.control
	 * </br>Class: ControlePrincipal
	 * </br>Method: getControleDisciplina
	 * </br>Last Change: 08/10/2011 11:22:42 by marcel
	 * </br>Description: Getter for ctrDisciplina
	 * </br>******************************</br>
	 * 
	 * @return the ctrDisciplina
	 */
	public ControleDisciplina getControleDisciplina() {

		return ctrDisciplina;
	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Trabalho04_13106.control
	 * </br>Class: ControlePrincipal
	 * </br>Method: run
	 * </br>Last Change: 08/10/2011 11:22:45 by marcel
	 * </br>Description: Method that call the menu and manage the users options
	 * </br>******************************</br>
	 * 
	 */
	public void run() {

		int nOperacao = -1;
		while (true) {
			nOperacao = limPrincipal.montaMenu();
			switch (nOperacao) {
				case 1:
					try {
						callDisciplinasOperations();
					} catch (Exception e) {
						limPrincipal
						        .showErrorMessage("Excecao gerada durante \no manuseio de disciplinas:\n\n"
						                + e.getMessage());
					}
					break;
				case 2:
					try {
						callEstudantesOperations();
					} catch (Exception e) {
						limPrincipal
						        .showErrorMessage("Excecao gerada durante \no manuseio de estudantes:\n\n"
						                + e.getMessage());
					}
					break;
				case 3:
					try {
						callTurmasOperations();
					} catch (Exception e) {
						limPrincipal
						        .showErrorMessage("Excecao gerada durante \no manuseio de turmas:\n\n"
						                + e.getMessage());
					}
					break;
				case 4:
					try {
						callAssentamentosOperations();
					} catch (Exception e) {
						limPrincipal
						        .showErrorMessage("Excecao gerada durante \no manuseio de turmas:\n\n"
						                + e.getMessage());
					}
					break;
				case 5:
					finalize();
			}
		}

	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Trabalho04_13106.control
	 * </br>Class: ControlePrincipal
	 * </br>Method: callDisciplinasOperations
	 * </br>Last Change: 08/10/2011 11:22:47 by marcel
	 * </br>Description: Method that call the options of Disciplina
	 * </br>******************************</br>
	 * 
	 * @throws Exception
	 */
	private void callDisciplinasOperations() throws Exception {

		ctrDisciplina.run();

	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Trabalho04_13106.control
	 * </br>Class: ControlePrincipal
	 * </br>Method: callEstudantesOperations
	 * </br>Last Change: 08/10/2011 11:22:49 by marcel
	 * </br>Description: Method that call the options of Estudante
	 * </br>******************************</br>
	 * 
	 * @throws Exception
	 */
	private void callEstudantesOperations() throws Exception {

		ctrEstudante.run();

	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Trabalho04_13106.control
	 * </br>Class: ControlePrincipal
	 * </br>Method: callTurmasOperations
	 * </br>Last Change: 08/10/2011 11:22:51 by marcel
	 * </br>Description: Method that call the options of Turma
	 * </br>******************************</br>
	 * 
	 * @throws Exception
	 */
	private void callTurmasOperations() throws Exception {

		ctrTurma.run();
	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Trabalho04_13106.control
	 * </br>Class: ControlePrincipal
	 * </br>Method: callAssentamentosOperations
	 * </br>Last Change: 08/10/2011 11:22:53 by marcel
	 * </br>Description: Method that call the options of Assentamento
	 * </br>******************************</br>
	 * 
	 * @throws Exception
	 */
	private void callAssentamentosOperations() throws Exception {

		ctrAssentamento.run();

	}


	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#finalize()
	 */
	@Override
	public void finalize() {

		try {
			ctrEstudante.finalize();

		} catch (Exception e) {
			limPrincipal
			        .showErrorMessage("Erro ao fechar arquivo estudantes.dat!\n"
			                + e.getMessage());
		}
		try {
			ctrDisciplina.finalize();
		} catch (Exception e) {
			limPrincipal
			        .showErrorMessage("Erro ao fechar arquivo disciplinas.dat!\n"
			                + e.getMessage());
		}
		try {
			ctrTurma.finalize();
		} catch (Exception e) {
			limPrincipal
			        .showErrorMessage("Erro ao fechar arquivo turmas.dat!\n"
			                + e.getMessage());
		}
		try {
			ctrAssentamento.finalize();
		} catch (Exception e) {
			limPrincipal
			        .showErrorMessage("Erro ao fechar arquivo assentamentos.dat!\n"
			                + e.getMessage());
		}

		System.exit(0);
	}

}
